console.log('777 :>> ', 777)
import Swiper from '../../lib/swiper/swiper-bundle.min';
require('./home.less')

require('../../lib/swiper/swiper-bundle.min.css')
require('../../assets/css/common.less')
const request = require('../../utils/request')
//引入tabbar渲染函数
const renderTabbar=require('../../utils/tabbar')
window.onload = () => {
  // 底部tabbar生成函数
  renderTabbar()
  // swiper部分
  let mySwiper = new Swiper('.swiper', {
    navigation: {
      nextEl: ".swiper-button-next",
      prevEl: ".swiper-button-prev",
    },
    loop: true, // 循环模式选项
    autoplay: {
      delay: 2500,
      disableOnInteraction: false,
    },//自动翻页

    // 如果需要分页器
    pagination: {
      el: ".swiper-pagination",
    },

  })
  // 
  let day = document.querySelector('.day')
  let accumlate = document.querySelector('.accumlate')
  let nowcard = document.querySelector('.nowcard')
  let munber = document.querySelector('.munber')
  let clock=document.querySelector('#clock')
  //获取打卡信息
  function getClockInfo() {
    request.get('/api/user/clockInInfo')
      .then(res => {
        // console.log(res.data);
      let {data,errno}=res.data
      if(errno===0){
        if(data.isClockIn){
          nowcard.textContent='今日已打卡'
          nowcard.className='nowcard'
        }else{
          nowcard.textContent='今日打卡'
          nowcard.className='nowcarded'
        }
      }

      })
  }
  getClockInfo()
  //获取用户信息
  function getUserInfo() {
    request.get('/api/user/info').then(res => {
      // console.log(res.data);
      let {data,errno}=res.data
      if(errno===0){
        render(data)

      }
   
    })
  }
  getUserInfo()
  // 渲染函数
  function render( data){
    day.textContent=data.clockCount
    munber.textContent=data.badges
    accumlate.innerHTML=`累计打卡${data.ranking}天`

  }
  // 点击打卡
  clock.addEventListener('click',()=>{
    request.post('/api/user/clockIn').then(res=>{
      console.log(res.data);
      let {errno,message}=res.data
      if(errno===0){
        getClockInfo()
        getUserInfo()
      }else{
        alert(message)
      }
      

    })
 
  })
}
